package janelas;

import javax.swing.JOptionPane;
import outros.Documento;
import persistencia.DAOUsuario;
import principal.Principal;
import transporte.TOUsuario;

public class Preferencias extends javax.swing.JInternalFrame implements JanelaPai {
    private DAOUsuario daoUsuario = new DAOUsuario();
    private JOptionPane msg;

    public Preferencias() {
        initComponents();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-getWidth())/2, (int)((screenSize.height-getHeight())/3), getWidth(), getHeight());
    }
    
    public void reiniciar() {
        limparCampos();
    }

    private void limparCampos() {
        camNovaSenha.setText("");
        camNovaSenhaConfirmar.setText("");
        camNovaSenha.requestFocus();
    }

    private boolean camposOk() {
        if (camNovaSenha.getText().length()==0) {
            msg.showMessageDialog(null, "Preencha o campo com a nova senha.", "ALERTA", msg.WARNING_MESSAGE);
            camNovaSenha.requestFocus();
            return false;
        } else if (camNovaSenhaConfirmar.getText().length()==0) {
            msg.showMessageDialog(null, "Preencha o campo de confirmação da nova senha.", "ALERTA", msg.WARNING_MESSAGE);
            camNovaSenhaConfirmar.requestFocus();
            return false;
        } else if (!camNovaSenha.getText().equals(camNovaSenhaConfirmar.getText())) {
            msg.showMessageDialog(null, "Digite a nova senha exatamente igual nos dois campos.", "ALERTA", msg.WARNING_MESSAGE);
            camNovaSenha.requestFocus();
            return false;
        } else {
            return true;
        }
    }

    private void redefinir() {
        if (camposOk()) {
            TOUsuario usuario = Principal.getUsuarioLogado();
            usuario.setSenha(camNovaSenha.getText());
            usuario.setStatus("ativo");
            daoUsuario.alterar(usuario);
            msg.showMessageDialog(null, "Senha alterada com sucesso.", "AVISO", msg.INFORMATION_MESSAGE);
            limparCampos();
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        camNovaSenha = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        camNovaSenhaConfirmar = new javax.swing.JPasswordField();
        botRedefinir = new javax.swing.JButton();

        setClosable(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Redefinir Senha", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI", 0, 13))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        jLabel1.setText("Nova senha");

        camNovaSenha.setDocument(new Documento(false, 20, false));
        camNovaSenha.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        camNovaSenha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camNovaSenhaActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        jLabel2.setText("Confirmar nova senha");

        camNovaSenhaConfirmar.setDocument(new Documento(false, 20, false));
        camNovaSenhaConfirmar.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        camNovaSenhaConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camNovaSenhaConfirmarActionPerformed(evt);
            }
        });

        botRedefinir.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        botRedefinir.setText("Redefinir");
        botRedefinir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botRedefinirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(camNovaSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(camNovaSenhaConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(botRedefinir))
                    .addComponent(jLabel2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(3, 3, 3)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(camNovaSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(camNovaSenhaConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botRedefinir))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botRedefinirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botRedefinirActionPerformed
        redefinir();
    }//GEN-LAST:event_botRedefinirActionPerformed

    private void camNovaSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camNovaSenhaActionPerformed
        redefinir();
    }//GEN-LAST:event_camNovaSenhaActionPerformed

    private void camNovaSenhaConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camNovaSenhaConfirmarActionPerformed
        redefinir();
    }//GEN-LAST:event_camNovaSenhaConfirmarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botRedefinir;
    private javax.swing.JTextField camNovaSenha;
    private javax.swing.JTextField camNovaSenhaConfirmar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}
